<template>
  <div>
    <el-dialog center :title="isAdd ? '添加商品分类' : '编辑商品分类'" :visible.sync="isShow" :close-on-press-escape="false"
      :before-close="offCheck">
      <el-form :model="cateInfo" :rules="rules" ref="cateForm">
        <el-form-item prop="pid" label="上级分类" :label-width="formLabelWidth">
          <el-select v-model="cateInfo.pid" placeholder="请选择">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option v-for="item in cateList" :key="item.id" :value="item.id" :label="item.catename"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="catename" label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="cateInfo.catename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" v-if="cateInfo.pid != 0" :label-width="formLabelWidth">
          <!-- 图片上传组件 -->
          <el-upload action="#" list-type="picture-card" :on-preview="onPreview" :on-remove="onRemove"
            :on-change="changeFile" :file-list="fileList" :limit="1" :on-exceed="handleExceed">
            <i class="el-icon-plus"></i>
          </el-upload>
          <!-- 图片预览组件 -->
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch v-model="cateInfo.status" active-color="#13ce66" inactive-color="#ccc" :active-value="1"
            :inactive-value="2">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="offCheck">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { cateAdd, cateInfo, cateEdit } from "../../request/api";
//引入辅助性函数
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      cateInfo: {
        pid: 0,
        catename: "",
        img: "",
        status: 1,
      },
      formLabelWidth: "120px",
      rules: {
        catename: [
          { required: true, message: "请输入商品分类名称", trigger: "blur" },
          {
            min: 2,
            max: 6,
            message: "长度在 2 到 6 个字符",
            trigger: "blur",
          },
        ],
        pid: [
          { required: true, message: "请选择上级商品分类", trigger: "change" },
        ],
      },
      dialogVisible: false,
      dialogImageUrl: "",
      fileList: [],
    };
  },
  computed: {
    ...mapGetters({
      cateList: "cate/getCateList",
    }),
  },
  methods: {
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length
        } 个文件`
      );
    },
    // 文件修改时触发
    changeFile(file, filelist) {
      this.cateInfo.img = file.raw;
    },
    // 图片预览
    onPreview(file) {
      //给预览图片赋值
      this.dialogImageUrl = file.url;
      //打开弹框
      this.dialogVisible = true;
    },
    // 图片删除
    onRemove(file, filelist) {
    },
    ...mapActions({
      getCate: "cate/getCateList_Action",
    }),

    // 点击取消
    offCheck() {
      // 子传父
      this.$emit("offCheck", false)
      // 清空表单信息
      this.$refs.cateForm.resetFields()
      this.cateInfo = {
        pid: 0,
        catename: "",
        img: "",
        status: 1,
      }
    },
    // 商品分类数据回显
    getOneCate(id) {
      // 调用查询一条商品分类信息接口
      cateInfo({ id }).then(res => {
        console.log(res);
        if (res.code == 200) {
          // 数据回显
          this.cateInfo = res.list
          this.cateInfo.id = id
          this.fileList = [{ url: this.$imgUrl + res.list.img }]
        }
      })
    },
    //点击确定
    addCate() {
      this.$refs.cateForm.validate((flag) => {
        if (flag) {
          //判断是否为添加还是编辑
          if (this.isAdd) {
            //调用添加接口
            cateAdd(this.cateInfo).then((res) => {
              if (res.code == 200) {
                //添加成功
                this.$message.success(res.msg);
                this.offCheck();
                // 重新渲染商品分类列表
                this.getCate();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            //调用编辑接口
            cateEdit(this.cateInfo).then((res) => {
              if (res.code == 200) {
                //添加成功
                this.$message.success(res.msg);
                this.offCheck();
                // 重新渲染商品分类列表
                this.getCate();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          // 错误提示
          this.$message.error("请输入正确的内容");
        }
      });
    },
  },
};
</script>

<style scoped>
</style>
